// pages/shop/goodsDetail/goodsDetail.js
const call = require('../../utils/request.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList:[''],
    autoplay: false,  //图片是否自动滚动
    indicatordots: false, //是否有小黑点
    duration: 500, //滑动时长
    currentIndex: 0, //当前图片
    goodsType:0, //商品详情/属性/推荐
    showModalStatus:false, //是否显示规格弹窗
    nowGG:0, //当前规格索引
    count:1, //购买数量
    buyType:null, //加入购物车0 立即购买1
    carCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取商品详情信息
    this.getDetail(options.id);
    //获取购物车数量
    this.getCarList();

    
  var windowHeight = parseInt(wx.getSystemInfoSync().windowHeight)*0.85
  this.setData({
    winH: windowHeight
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
        // 来⾃⻚⾯内转发按钮
        console.log(res.target)
    }
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length-1] //获取当前页面的对象
    var url = currentPage.route //当前页面url
    var options = currentPage.options//如果要获取url中所带的参数

    return {
        title: this.data.data.product_name,
        path: `${url}?id=${options.id}`,
        imageUrl:"",
        success:function (res) {
            // 转发成功
            console.log("成功",res);
        },
        fail: function (res) {
            // 转发失败
            console.log("失败",res)
        }
    }
  },
  

  /**
   * 修改数量 
   */
  changeNum(e){
    const type = e.currentTarget.dataset.type;
    let count = this.data.count;
    if(type=="del"){count = count==1?count:count-1;}
    else{count++;}
    this.setData({
      count:count
    })
  },

  /**
   * 图片滑动
   */
  onSlideChange: function (event) { 
    var postId = event.detail.current; 
    this.setData({
      currentIndex: postId
    })
  },

  /**
   * 选择商品详情/属性/推荐
   */
  changeType(e){
    const type = e.currentTarget.dataset.type;
    this.setData({
      goodsType: type
    })
  },

  /**
   * 获取商品详情
   */
  getDetail(id){
    wx.showLoading({
      title: '正在加载',
      mask:true
    })
    call.post('/api/shop/goods_detail',{
      product_id: id
    }).then(res=>{
      console.log("获取商品详情",res.data.data)
      const data = res.data.data;
      let obj = {data: data};
      let imgs = data.images;
      if(imgs.length>0){
        obj = Object.assign(obj,{
          imgList:imgs
        })
      }
      this.setData(obj)
      wx.hideLoading();
    })
  },

  /**
   * 添加商品到购物车
   */
  addToCar(){
    wx.showLoading({
      title: '正在添加',
      mask:true
    })
    call.post('/api/shop/add_to_cart',{
      product_id: this.data.data.id,
      product_spec_id: this.data.data.product_specific[this.data.nowGG].id,
      count: this.data.count
    }).then(res=>{
      wx.hideLoading();
      console.log(res.data)
      if(res.data.code == 0){
        wx.showToast({
          title: '已添加购物车',
        })
        this.hideModal();
        this.getCarList();
      }
    })
  },

  /**
   * 页面跳转
   */
  toUrl(e){
    //判断是否登录
    if(app.globalData.isBind == 0){
      wx.reLaunch({
        url:'../../pages/login/index/index'
      });
      return;
    }
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },

  /**
   * 客服咨询
   */
  callPhone(){
    call.phone();
  },

  /**
   * 规格选择
   */
  chooseGG(e){
    const nowGG = e.currentTarget.dataset.index;
    this.setData({
      nowGG:nowGG
    })
  },

  /**
   * 确认规格 加入购物车或者购买 
   */
  sureToBuy(e){ 
    //判断是否登录
    if(app.globalData.isBind == 0){
      wx.reLaunch({
        url:'../../pages/login/index/index'
      });
      return;
    }
    const type = e.currentTarget.dataset.type;
    if(type == 0){ //加入购物车
      this.addToCar();
    }else if(type == 1){ //立即购买
      const data = this.data.data;
      const index = this.data.nowGG;
      let arr = [{
        product_id:data.id,
        product_spec_id:data.product_specific[index].id,
        count:this.data.count,
        product_name:data.product_name,
        img_thum:data.img_thum,
        price:data.product_specific[index].price,
        specification:data.product_specific[index].specification,
        is_vip_product:data.is_vip_product,
        ship_fee_template:data.ship_fee_template
      }]
      app.globalData.goodsList = arr;
      wx.navigateTo({
        url: '../confirm/confirm',
      })
    }
  },

  //获取购物车总数
  getCarList(){
    call.post('/api/shop/cart_goods_num',{
      page: this.data.pageIndex
    }).then(res=>{
      console.log("获取购物车数量",res.data.data)
      wx.hideLoading();
      this.setData({
        carCount:res.data.data
      })
    })
  },

  //显示规格弹窗
  showModal: function (e) {
    //加入购物车0 立即购买1
    const type =  e.currentTarget.dataset.type;
    this.setData({
      buyType:type
    })
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(723).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏规格弹窗
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  
  
})